package com.li.mybatisconfig.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.li.mybatisconfig.config.MyConfiguration;
import com.li.mybatisconfig.entity.User;
import com.li.mybatisconfig.service.Excutor;
import netscape.javascript.JSObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.Callable;

/**
 * @Description：
 * @Author：LiDong
 * @Create：2020/9/2
 * @Version：1.0.0
 */
public class ExcutorImpl implements Excutor {

    private Logger logger= LoggerFactory.getLogger(ExcutorImpl.class);

    private MyConfiguration myConfiguration=new MyConfiguration();

    Connection connection = myConfiguration.getDataSourceInfo();

    @Override
    public <T> T query(String statement, Object parameter) {
        try{
            PreparedStatement preparedStatement = connection.prepareStatement(statement);
            preparedStatement.setString(1,parameter.toString());
            ResultSet resultSet = preparedStatement.executeQuery();
            User user=new User();
            while (resultSet.next()){
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setGender(resultSet.getInt("gender"));
                user.setAge(resultSet.getInt("age"));
                user.setIdcard(resultSet.getInt("idcard"));
                user.setPhone(resultSet.getInt("phone"));
            }
            logger.info("遍历结果集完成！");
            return (T)user;
        }catch (Exception e){
            logger.error("查询结果报错"+e.getMessage());
            return null;
        }
    }

    @Override
    public void save(String statement, Object parameter) {
        try {
            connection.setAutoCommit(false);
            PreparedStatement preparedStatement = connection.prepareStatement(statement);
            User user = JSON.parseObject(String.valueOf(parameter),User.class);
            preparedStatement.setInt(1,user.getId());
            preparedStatement.setString(2,user.getUsername());
            preparedStatement.setString(3,user.getPassword());
            preparedStatement.setInt(4,user.getGender());
            preparedStatement.setInt(5,user.getAge());
            preparedStatement.setInt(6,user.getIdcard());
            preparedStatement.setInt(7,user.getPhone());

            preparedStatement.execute();
            comit(connection);
            logger.info("保存成功！");
        } catch (Exception e ) {
            logger.error("保存失败"+e.getMessage());
            rollBack(connection);
        }
    }

    @Override
    public void modify(String statement, Object parameter) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(statement);
            User user = JSON.parseObject(String.valueOf(parameter),User.class);
            preparedStatement.setInt(3,user.getId());
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());

            preparedStatement.execute();
            comit(connection);
            logger.info("修改成功！");

        } catch (SQLException e) {
            logger.error("修改失败"+e.getMessage());
            rollBack(connection);
        }
    }

    @Override
    public void remove(String statement, Object parameter) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(statement);
            preparedStatement.setString(1,parameter.toString());
            preparedStatement.execute();
            logger.info("删除成功！");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }


    /**
     * 事物提交
     */
    private void comit(Connection connection){
        try {
            connection.commit();
            logger.error("事物提交成功！");
        }catch (Exception e){
            logger.error("事物提交失败！"+e.getMessage());
        }
    }

    /**
     * 事物回滚
     */
    private void rollBack(Connection connection){
        try{
            connection.rollback();
            logger.info("事物回滚成功！");
        } catch (Exception e){
            logger.info("事物回滚失败！"+e.getMessage());
        }
    }

}
